/** * Copyright (c) 2016-2019 人人开源 All rights reserved. */

package com.joinval.modules.sys.controller;

import com.joinval.common.utils.R;
import com.joinval.common.utils.Result;
import com.joinval.modules.sys.entity.SysUserEntity;
import com.joinval.modules.sys.form.BaseWxLoginForm;
import com.joinval.modules.sys.form.SysLoginForm;
import com.joinval.modules.sys.service.SysCaptchaService;
import com.joinval.modules.sys.service.SysUserService;
import com.joinval.modules.sys.service.SysUserTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 登录相关
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@Api(description = "登陆相关")
public class SysLoginController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserTokenService sysUserTokenService;
	@Autowired
	private SysCaptchaService sysCaptchaService;


	/**
	 * 验证码
	 */
	@GetMapping("captcha.jpg")
	public void captcha(HttpServletResponse response, String uuid) throws IOException {
		response.setHeader("Cache-Control", "no-store, no-cache");
		response.setContentType("image/jpeg");

		//获取图片验证码
		BufferedImage image = sysCaptchaService.getCaptcha(uuid);

		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		IOUtils.closeQuietly(out);
	}

	/**
	 * PC登录
	 */
	@ApiOperation(value = "web登录")
	@PostMapping("/sys/login")
	public Map<String, Object> login(@RequestBody SysLoginForm form) throws IOException {
/*		boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
		if(!captcha){
			return R.error("验证码不正确");
		}*/

		//用户信息
		SysUserEntity user = sysUserService.queryByUserName(form.getUsername());

		//账号不存在、密码错误
		if (user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
			return R.error("账号或密码不正确");
		}

		//账号锁定
		if (user.getStatus() == 0) {
			return R.error("账号已被锁定,请联系管理员");
		}

		//生成token，并保存到数据库
		R r = sysUserTokenService.createToken(user.getUserId());
		return r;
	}

	/**
	 * 小程序用户名密码方式登录
	 */
	@ApiOperation(value = "小程序用户名密码方式登录")
	@PostMapping("/sys/appletLogin")
	public Map<String, Object> appletLogin(@RequestBody SysLoginForm form) throws IOException {
		//用户信息
		SysUserEntity user = sysUserService.queryByUserName(form.getUsername());
		//账号不存在、密码错误
		if (user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
			return R.error("账号或密码不正确");
		}
		//账号锁定
		if (user.getStatus() == 0) {
			return R.error("账号已被锁定,请联系管理员");
		}
		//无角色
		List<Map<String, Object>> roles =  sysUserService.getUserRolesByUserId(user.getUserId());
		if (roles.size()<=0){
			return R.error("无权限访问");
		}

		//生成token，并保存到数据库
		R r = sysUserTokenService.createToken(user.getUserId());
		return r;
	}

	@PostMapping("/sys/wxLogin")
	@ApiOperation(value = "微信登陆")
	public Map<String, Object> wxLogin(@RequestBody @ApiParam(name = "BaseWxLoginForm", value = "微信用户基础信息", required = true) BaseWxLoginForm baseWxLoginForm) {
		return sysUserService.wxLogin(baseWxLoginForm);
	}


	@GetMapping("/sys/wxLoginUser")
	@ApiOperation(value = "微信用户实时信息接口", notes = "微信界面用户信息查询接口")
	public Result wxLoginUser(@RequestParam Long userId) {
		return sysUserService.wxLoginUser(userId);
	}

	/**
	 * 退出
	 */
	@ApiOperation(value = "注销")
	@PostMapping("/sys/logout")
	public R logout() {
		sysUserTokenService.logout(getUserId());
		return R.ok();
	}

	@ApiOperation(value = "微信小程序获取照片")
	@GetMapping(value = "/wxImage")
	public Result wxImage(@RequestParam String verifyResult) {
		return sysUserService.wxImage(verifyResult);
	}


}
